import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';
import 'package:seedfour/app/constants.dart';
import 'package:seedfour/app/theme.dart';
import 'package:seedfour/app/views/module_main/main_home/widget/home_case.dart';
import 'package:seedfour/app/views/module_main/main_home/widget/home_navigation.dart';

import 'logic.dart';

class HomePage extends StatelessWidget {
  final String title;

  HomePage({
    Key? key,
    required this.title,
  }) : super(key: key);

  final logic = Get.put(HomeLogic());
  final state = Get.put(HomeLogic()).state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Obx(
          () => Row(
            children: [
              state.iconUrl.value.isEmpty
                  ? SvgPicture.asset(
                      'assets/icons/ic_circle_logo.svg',
                      width: 25.w,
                      height: 25.w,
                    )
                  : Image.network(
                      state.iconUrl.value,
                      width: 25.w,
                      height: 25.w,
                    ),
              SizedBox(width: kSpaceMini),
              Expanded(
                child: Text(
                  state.title.value,
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(
                    height: 1.2,
                    fontSize: kFontSizeMedium,
                    color: AppColor.colorFontBlack,
                  ),
                ),
              ),
            ],
          ),
        ),
        actions: [
          IconButton(
            onPressed: () => logic.toAIPage(),
            icon: SvgPicture.asset(
              'assets/icons/ic_ai.svg',
              width: kIconSizeTiny,
            ),
          ),
          IconButton(
            onPressed: () => {logic.toMessageList()},
            icon: Obx(() {
              return state.messageCount.value > 0
                  ? Badge.count(
                      backgroundColor: AppColor.colorTipsRed,
                      textColor: Colors.white,
                      count: state.messageCount.value,
                      child: SvgPicture.asset(
                        'assets/icons/ic_email.svg',
                        width: kIconSizeTiny,
                      ),
                    )
                  : SvgPicture.asset(
                      'assets/icons/ic_email.svg',
                      width: kIconSizeTiny,
                    );
            }),
          ),
          // IconButton(
          //   onPressed: () => {},
          //   icon: SvgPicture.asset(
          //     'assets/icons/ic_search.svg',
          //     width: kIconSizeTiny,
          //   ),
          // ),
        ],
      ),
      body: RefreshIndicator(
        onRefresh: () async => logic.reload(),
        child: ListView(
          controller: logic.scrollController,
          children: [
            SizedBox(height: kSpaceSmall),
            HomeNavigation(
              data: state,
              onClickButton: (item) {
                if (item == state.typePrint) {
                  _showBottomSheet();
                } else {
                  logic.clickNavigationButton(item);
                }
              },
            ),
            HomeCase(
              data: state,
              onClickMore: () => logic.toNoticeList(),
              onClickItem: (item) => logic.toNoticeDetail(item),
            ),
            _buildMoreTip(),
          ],
        ),
      ),
      floatingActionButton: InkWell(
        onTap: () => logic.toCreateNotice(),
        splashColor: Colors.transparent,
        highlightColor: Colors.transparent,
        child: Image.asset(
          'assets/buttons/create_notice.png',
          width: 109.w,
          height: 50.w,
        ),
      ),
    );
  }

  Widget _buildMoreTip() {
    return Center(
      child: Padding(
        padding: EdgeInsets.all(kSpaceMedium),
        child: Obx(
          () => Text(
            state.bottomTips.value,
            style: TextStyle(fontSize: kFontSizeSmall, color: Colors.grey),
          ),
        ),
      ),
    );
  }

  // 底部关联案件弹窗
  void _showBottomSheet() {
    showModalBottomSheet(
      backgroundColor: Colors.white,
      context: Get.context!,
      isDismissible: false,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.vertical(top: Radius.circular(15.w)),
      ),
      builder: (context) {
        return SizedBox(
          width: double.infinity,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Padding(
                padding: EdgeInsets.only(
                  left: kSpaceMedium,
                  top: kSpaceTiny,
                  bottom: kSpaceTiny,
                  right: kSpaceTiny,
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      '请选择申请类型',
                      style: TextStyle(
                        fontSize: kFontSizeMedium,
                        color: AppColor.colorFontBlack,
                      ),
                    ),
                    IconButton(
                      onPressed: () => {Get.back()},
                      icon: SvgPicture.asset(
                        'assets/icons/ic_cancel_black.svg',
                        width: kIconSizeMini,
                      ),
                    ),
                  ],
                ),
              ),
              const Divider(),
              Expanded(
                child: ListView.builder(
                  itemCount: SealPaperType.values.length,
                  itemBuilder: (context, index) {
                    if (SealPaperType.values[index].value == 0 ||
                        SealPaperType.values[index].value == 9) {
                      return Container();
                    }

                    return ListTile(
                      title: Text(
                        SealPaperType.values[index].label,
                        style: TextStyle(
                          fontSize: kFontSizeSmall,
                          color: AppColor.colorFontBlack,
                        ),
                      ),
                      trailing: Obx(
                        () => logic.createPaperType.value ==
                                SealPaperType.values[index]
                            ? SvgPicture.asset(
                                'assets/icons/ic_radio_selected.svg',
                                width: 16.w)
                            : SvgPicture.asset(
                                'assets/icons/ic_radio_unselect.svg',
                                width: 16.w),
                      ),
                      onTap: () => logic
                          .onSelectedCreateType(SealPaperType.values[index]),
                    );
                  },
                ),
              ),
              const Divider(),
              Padding(
                padding: EdgeInsets.all(kSpaceMedium),
                child: Obx(
                  () => FilledButton(
                    onPressed: logic.createPaperType.value == null
                        ? null
                        : () => {Get.back(), logic.toCreateSealApply()},
                    child: Container(
                      alignment: Alignment.center,
                      width: double.infinity,
                      height: 50.w,
                      child: Text(
                        '确认',
                        textAlign: TextAlign.center,
                        style: TextStyle(fontSize: kFontSizeMedium),
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }
}
